package com.shopping.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.auto.JSONAuthentication;
import com.shopping.entity.ShNotice;
import com.shopping.entity.ShSalaryEx;
import com.shopping.entity.ShSellEx;
import com.shopping.mapper.ShSalaryExMapper;
import com.shopping.mapper.ShSellExMapper;
import com.shopping.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shopping
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/sh-sell-ex")
public class ShSellExController extends JSONAuthentication {
    @Autowired
    ShSellExMapper shSellExMapper;

    @ApiOperation(value = "添加销售记录")
    @PostMapping("/add")
    public void addOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("productId") int  productId, @RequestParam("num") int num,@RequestParam("price") float price) throws  Exception{
        ShSellEx  shSellEx=new ShSellEx();
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        shSellEx.setProductId(productId);
        shSellEx.setPrice(price);
        shSellEx.setNum(num);
        shSellEx.setCreatTime(timestamp);
        if(shSellExMapper.insert(shSellEx)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部销售记录")
    @PostMapping("/findAll")
    public void findAll(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam ("current") int current)throws  Exception{
        Page<ShSellEx> shUserPage=new Page<>(current,size);
        Page<ShSellEx> shUserPages=shSellExMapper.selectPage(shUserPage,null);
        List<ShSellEx> records = shUserPages.getRecords();
        if(records.size()>0){
            Result ok = Result.ok().data("xiaoshou",records);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

